//
//  YBActivationCodePopupVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2024/1/4.
//  Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import HandyJSON
import SwiftyJSON
import SDWebImage

class YBActivationCodePopupVC: BaseMaskVC {
    
    var onDismiss: (() -> Void)?
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let line = UIView(color: ColorDef.gray153)
        view.addSubview(bgImgView)
        bgImgView.addSubviews([titleLabel, closeBtn, inputBgView, exchangeImgView,
                               line, textField, cancelBtn, exchangeBtn])
        
        bgImgView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(kScreenWidth*0.693)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(16).priority(.high)
            make.height.equalTo(18)
        }
        
        closeBtn.snp.makeConstraints { make in
            make.size.equalTo(15)
            make.right.equalTo(-10)
            make.top.equalTo(12)
        }
        
        inputBgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(28)
            make.left.right.equalToSuperview().inset(25)
            make.height.equalTo(30)
        }
        
        exchangeImgView.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(18, 16))
            make.centerY.equalTo(inputBgView)
            make.left.equalTo(inputBgView.snp.left).offset(9)
        }
        
        line.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(0.5, 11.5))
            make.centerY.equalTo(exchangeImgView)
            make.left.equalTo(exchangeImgView.snp.right).offset(9.5)
        }
        
        textField.snp.makeConstraints { make in
            make.centerY.equalTo(inputBgView)
            make.right.equalTo(inputBgView.snp.right).offset(-7)
            make.left.equalTo(line.snp.right).offset(10)
        }
        
        cancelBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake((kScreenWidth*0.693-90)/2, 30))
            make.top.equalTo(inputBgView.snp.bottom).offset(23)
            make.left.equalTo(inputBgView)
            make.bottom.equalTo(-35)
        }
        
        exchangeBtn.snp.makeConstraints { make in
            make.size.centerY.equalTo(cancelBtn)
            make.right.equalTo(inputBgView)
        }
    }
    
    // MARK: - lazy
    lazy var vm: YBMineViewModel = {
        let vm = YBMineViewModel()
        return vm
    }()
    
    private lazy var bgImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_popup_bg"))
        iv.isUserInteractionEnabled = true
        iv.bk_addCornerBorder(radius: 8, borderWidth: 0.1, borderColor: .clear)
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var closeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "icon_popup_close"), for: .normal)
        btn.bk_addTarget { [weak self] sender in
            self?.dismiss(animated: true, completion: nil)
        }
        return btn
    }()
    
    private lazy var titleLabel = self.bk_addLabel(text: "兑换激活码", font: .systemFont(ofSize: 16, weight: .medium), bgColor: .clear, textColor: ColorDef.dark27, align: .center)
    
    private lazy var inputBgView: UIView = {
        let view = UIView()
        view.bk_addCornerBorder(radius: 8, borderWidth: 0.5, borderColor: ColorDef.gray229)
        return view
    }()
    
    private lazy var exchangeImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_popup_exchange"))
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var exchangeBtn: UIButton = {
        let btn = self.bk_addButton(type: .custom, title: "兑换", font: .systemFont(ofSize: 13), bgColor: ColorDef.dark27, titleColor: .white, radius: 8)
        
        btn.bk_addTarget { [weak self] sender in
            guard let self = self else { return }
            self.textField.resignFirstResponder()
            
            guard let codeString = self.textField.text, codeString.isValid else {
                HintTool.toast("请输入兑换码")
                return
            }
            
            self.vm.req_ExchangeCode(codeString) { [weak self] ok, obj, msg, code in
                if ok {
                    HintTool.toast("兑换成功")
                    self?.dismiss(animated: true, completion: {
                        self?.onDismiss?()
                    })
                } else {
                    HintTool.toast(msg)
                }
            }
        }
        return btn
    }()
    
    private lazy var cancelBtn: UIButton = {
        let btn = self.bk_addButton(type: .custom, title: "取消", font: .systemFont(ofSize: 13), bgColor: ColorDef.gray184, titleColor: kRGBColor(19, 25, 44), radius: 8)
        btn.bk_addTarget { [weak self] sender in
            self?.dismiss(animated: true)
        }
        return btn
    }()
    
    private lazy var textField: UITextField = {
        let tf = UITextField()
        tf.borderStyle = .none
        tf.bk_addPlaceholder("请输入兑换码")
        tf.backgroundColor = .clear
        tf.textColor = ColorDef.dark27
        tf.textAlignment = .left
        tf.font = .systemFont(ofSize: 15)
        return tf
    }()
    
}
